#!/usr/bin/env python3
"""
配置验证脚本
"""
import sys
from pathlib import Path

# 添加项目根目录到Python路径
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))

from config.loaders import get_config, validate_config

def main():
    """主验证函数"""
    print("🔍 RedFire 统一配置验证")
    print("=" * 50)
    
    try:
        # 加载配置
        print("📥 加载配置...")
        config = get_config()
        print(f"✅ 配置加载成功 (版本: {config.version})")
        
        # 验证配置
        print("\n🔬 验证配置...")
        validation_result = validate_config(config)
        
        # 显示验证结果
        print(f"\n📊 验证结果:")
        print(f"  配置有效性: {'✅ 有效' if validation_result['valid'] else '❌ 无效'}")
        print(f"  总服务数: {validation_result['summary']['total_services']}")
        print(f"  启用服务数: {validation_result['summary']['enabled_services']}")
        print(f"  数据库数: {validation_result['summary']['total_databases']}")
        print(f"  错误数: {validation_result['summary']['error_count']}")
        print(f"  警告数: {validation_result['summary']['warning_count']}")
        
        # 显示启用的服务
        print(f"\n🚀 启用的服务:")
        enabled_services = config.get_enabled_services()
        for service_name, service in enabled_services.items():
            print(f"  - {service.name}: http://{service.host}:{service.port}")
        
        # 显示数据库配置
        print(f"\n💾 数据库配置:")
        for db_name, db in config.databases.items():
            print(f"  - {db_name}: {db.type} @ {db.host}:{db.port}")
        
        # 显示错误
        if validation_result['errors']:
            print(f"\n❌ 配置错误:")
            for error in validation_result['errors']:
                print(f"  - {error}")
        
        # 显示警告
        if validation_result['warnings']:
            print(f"\n⚠️  配置警告:")
            for warning in validation_result['warnings']:
                print(f"  - {warning}")
        
        # 返回结果
        return 0 if validation_result['valid'] else 1
        
    except Exception as e:
        print(f"❌ 配置验证失败: {e}")
        return 1

if __name__ == "__main__":
    exit_code = main()
    sys.exit(exit_code)
